<script lang="ts" setup>
import BaseButton from "@/components/BaseButton/index.vue";
import BaseUpDown from "@/components/BaseUpDown/index.vue";
import DateRange from "@/components/DateRange/index.vue";
import useSearch from "@/hooks/useSearch";
import useData from "./hooks/useData";
import { useUserStore } from "@/stores/modules/user";
import { formatDate } from "@/utils";

const { pageData, agrMap } = useData();
import { deleteData, getDataByPage } from "@/api/modules/control-strategy/policy";
import UpdateForm from "./components/UpdateForm/index.vue";

const userStore = useUserStore();
const username = computed(() => userStore.userInfo.name);
const { tableData, loadData, formData, reloadData } = useSearch({
  defaultData: {
    ipproto: "0"
  },
  responseKey: {
    list: "data",
    total: "total"
  }
});

function loadDataCb() {
  loadData({
    api: getDataByPage,
    sendData: {
      curpage: formData.pageNum - 1,
      perpage: formData.pageSize,
      startTime: formData.startTime ? parseInt(new Date(formData.startTime).getTime() / 1000) : undefined,
      endTime: formData.endTime ? parseInt(new Date(formData.endTime).getTime() / 1000) : undefined,
      policystr: formData.policystr,
      iswhite: 0
    }
  });
}

loadDataCb();

function reloadDataCb() {
  reloadData();
  loadData({
    api: getDataByPage,
    sendData: {
      curpage: formData.pageNum - 1,
      perpage: formData.pageSize,
      iswhite: 0
    }
  });
}

loadDataCb();

/**删除*/
import useDelete from "@/hooks/useDelete";
import { computed } from "vue";
import BaseCollapse from "@/components/BaseCollapse/index.vue";

const { deleteHandler, idList } = useDelete();

async function deleteCb(id, pid, type) {
  await deleteHandler({
    api: deleteData,
    multiple: false,
    sendData: {
      id: id,
      pid: [pid],
      type: type,
      iswhite: 0
    },
    hideMessageBox: false
  });
  loadDataCb();
}

function selectCb(data) {
  console.log(data);
  idList.value = data.rowList.map(item => item.userId);
}
</script>

<template>
  <BaseUpDown>
    <template #up>
      <BaseCollapse title="">
        <template #header>
          <div class="h-box justify-end" style="width: 150px; float: right" @click.stop>
            <UpdateForm @success="loadDataCb()">
              <BaseButton type="add_icon_text" />
            </UpdateForm>
            <BaseButton @click="reloadDataCb()" type="refresh_icon_text" />
          </div>
        </template>
        <el-form>
          <div class="h-box" style="flex-wrap: wrap">
            <el-form-item label="关键字">
              <el-input v-model="formData.policystr" placeholder="IP/MAC/域名" style="width: 160px" />
            </el-form-item>
            <el-form-item class="mg-l-sm" label="时间">
              <DateRange v-model:start="formData.startTime" v-model:end="formData.endTime" width="360" />
            </el-form-item>
            <BaseButton class="mg-l-sm" style="margin-bottom: 18px" @click="loadDataCb()" type="search_icon_text" />
            <BaseButton class="mg-l-sm" style="margin-bottom: 18px" @click="reloadDataCb()" type="reset_icon_text" />
          </div>
        </el-form>
      </BaseCollapse>
    </template>
    <he-table
      @handle-current-change="loadDataCb"
      :total="tableData.total"
      v-model:page-size="formData.pageSize"
      v-model:page-num="formData.pageNum"
      operate-width="180px"
      :operate-list="[]"
      :table-data="tableData.list"
      :column-data="pageData.columnData"
      :show-multiple="false"
      :show-summary="false"
      :show-operate="false"
      :show-multiple-column="false"
      :show-pagination="true"
      @select="selectCb"
    >
      <template #typeDefault="{ data }">
        <span v-if="data.row.type == 1">IP</span>
        <span v-if="data.row.type == 2">MAC</span>
        <span v-if="data.row.type == 3">域名</span>
      </template>
      <template #8Default="{ data }">
        {{ formatDate(data.row[8]) }}
      </template>
      <template #4Default="{ data }">
        {{ agrMap.get(data.row[4]) }}
      </template>
      <template #operationDefault="{ data }">
        <UpdateForm
          :row-data="{
            ...data.row
          }"
          :title="`编辑`"
          @success="loadDataCb"
        >
          <base-button type="edit_text"></base-button>
        </UpdateForm>
        <base-button @click="deleteCb(data.row.id, data.row.pid, data.row.type)" type="del_text"></base-button>
      </template>
    </he-table>
  </BaseUpDown>
</template>
